﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL_Class;

namespace NLLSite.Administrator.UserControl
{
    public partial class ManageCourses : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnXoa.Visible = false;
            CheckBox3.Visible = false;
            Button3.Visible = false;
            Panel2.Visible = true;
            Session["IDCourse"] = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
            txtTenKhoaHoc.Text = HttpUtility.HtmlDecode(GridView1.SelectedRow.Cells[2].Text);
            txtThumbImage.Text = HttpUtility.HtmlDecode(((Label)GridView1.SelectedRow.FindControl("Label3")).Text);
            txtDescription.Text = HttpUtility.HtmlDecode(((Label)GridView1.SelectedRow.FindControl("Label4")).Text);
            CKEditor1.Text = HttpUtility.HtmlDecode(((Label)GridView1.SelectedRow.FindControl("Label2")).Text);
            txtSoLuong.Text = HttpUtility.HtmlDecode(GridView1.SelectedRow.Cells[4].Text);
            dpkBeginDate.SelectedDate = Convert.ToDateTime(((Label)GridView1.SelectedRow.FindControl("Label1")).Text);
            txtLichHoc.Text = HttpUtility.HtmlDecode(GridView1.SelectedRow.Cells[6].Text);
            txtGioHoc.Text = HttpUtility.HtmlDecode(GridView1.SelectedRow.Cells[7].Text);
            bool b = ((CheckBox)GridView1.SelectedRow.Cells[8].Controls[0]).Checked;
            if (b == true)
            {
                CheckBox1.Checked = true;
            }
            else
            {
                CheckBox1.Checked = false;
            }
            bool c = ((CheckBox)GridView1.SelectedRow.Cells[9].Controls[0]).Checked;
            if (c==true)
            {
                CheckBox2.Checked = true;
            }
            else
            { 
                CheckBox2.Checked = false;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            CheckBox3.Visible = true;
            btnXoa.Visible = true;
            Panel2.Visible = false;
            Button3.Visible = true;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            KhoaHoc db = new KhoaHoc();
            Course cou = new Course();

            cou.ID = Int32.Parse(Session["IDCourse"].ToString());
            cou.Name = txtTenKhoaHoc.Text;
            cou.ThumbImage = txtThumbImage.Text;
            cou.Description = txtDescription.Text;
            cou.Content = CKEditor1.Text;
            cou.Quantities = Int32.Parse(txtSoLuong.Text);
            cou.StartDate = dpkBeginDate.SelectedDate;
            cou.Schedule = txtLichHoc.Text;
            cou.TimeSlot = txtGioHoc.Text;
            cou.IsFrontPage = CheckBox1.Checked;
            cou.Status = CheckBox2.Checked;

            db.UpdateCourse(cou);
            CheckBox3.Visible = true;
            btnXoa.Visible = true;
            Panel2.Visible = false;
            Button3.Visible = true;
            GridView1.DataBind();
        }

        protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("cbDelete");
                if (CheckBox3.Checked)
                {
                    cb.Checked = true;
                }
                else
                {
                    cb.Checked = false;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            KhoaHoc db = new KhoaHoc();
            
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("cbDelete");
                if (cb != null && cb.Checked)
                {
                    db.DeleteCourse(Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value));
                }
            }
            GridView1.DataBind();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThemKhoaHoc.aspx");
        }
    }
}